// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Touch Block Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 69 : TYPE_TOUCHBLOCK

// Function declarations
reserve function TouchBlock_DebugDraw
reserve function TouchBlock_DebugSpawn

// Static Values

// Tables

function TouchBlock_DebugDraw
	DrawSprite(0)
end function


function TouchBlock_DebugSpawn
	CreateTempObject(TypeName[Touch Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].value3 = object.ypos
end function


event ObjectMain
	temp0 = object.ypos
	temp0 &= -0x10000
	switch object.state
	case 0
		break
	case 1
		object.ypos += object.yvel
		object.yvel += 0x800
		ObjectTileCollision(CSIDE_FLOOR, 0, 16, 0)
		if checkResult == 1
			object.state++
		end if
		if object.outOfBounds == 1
			object.ypos = object.value3
			object.yvel = 0
			object.value2 = 0
			object.state = 3
		end if
		break
	case 2
		if object.outOfBounds == 1
			object.ypos = object.value3
			object.yvel = 0
			object.value2 = 0
			object.state++
		end if
		break
	case 3
		if object.outOfBounds == 1
			object.state = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		break
	end switch
	object.value1 = object.ypos
	object.value1 &= -0x10000
	object.value1 -= temp0
	object.value0 = 0
	if object.state < 3
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0					
				BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 1
					object[currentPlayer].ypos += object.value1
					break
				case 2
				case 3
					if object.state == 0
						object.state = 1
					end if
					break
				case 4
					if object[currentPlayer].gravity == GRAVITY_GROUND
						CallFunction(PlayerObject_Kill)
					end if
					break
				end switch
			end if
			if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
				if object[0].value1 >= 17
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						if object[currentPlayer].xpos > object.xpos
							object[currentPlayer].ixpos = object[currentPlayer].collisionLeft
							FlipSign(object[currentPlayer].xpos)
							object[currentPlayer].xpos += 0x100000
							object[currentPlayer].xpos += object.xpos
							if object[currentPlayer].xvel < 0
								object[currentPlayer].pushing = 2
								object[currentPlayer].xvel = 0
								object[currentPlayer].speed = 0
							end if
						else
							object[currentPlayer].ixpos = object[currentPlayer].collisionRight
							FlipSign(object[currentPlayer].xpos)
							object[currentPlayer].xpos -= 0x100000
							object[currentPlayer].xpos += object.xpos
							if object[currentPlayer].xvel > 0
								object[currentPlayer].pushing = 2
								object[currentPlayer].xvel = 0
								object[currentPlayer].speed = 0
							end if
						end if
					end if
				end if					
			end if					
		next
		object.ypos = temp1
	end if
end event


event ObjectDraw
	if object.state < 3
		DrawSprite(0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 84, 223)
	foreach (TypeName[Touch Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value3 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[Touch Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(TouchBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(TouchBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
